package results

import "gitee.com/lailonghui/vehicle-supervision-framework/pkg/errors"

// 返回结果
type Result struct {
	Msg      string      `json:"msg"`
	Code     int         `json:"code"`
	HttpCode int         `json:"-"`
	Data     interface{} `json:"data"`
}

func Suc(data interface{}) *Result {
	return &Result{
		Msg:      SUCCESS_MSG,
		Code:     SUCCESS,
		HttpCode: 200,
		Data:     data,
	}
}

func Err(err error) *Result {
	appError, ok := err.(errors.AppError)
	if !ok {
		appError = errors.New(err.Error())
	}
	if appError.Code == 0 {
		appError.Code = FAIL
	}

	return &Result{
		Msg:      appError.Msg,
		Code:     appError.Code,
		HttpCode: appError.HttpCode,
		Data:     nil,
	}
}
